// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.controllers' is found in controllers.js

angular.module('FirstAppKit', ['ionic', 'starter.controllers'])

    .run(function ($ionicPlatform) {
        "use strict";
        $ionicPlatform.ready(function () {
            if (window.StatusBar) {
                // org.apache.cordova.statusbar required
                StatusBar.styleDefault();
            }
            function onDeviceReady() {
                var db = window.openDatabase("FirstAidDatabase", "1.0", "PhoneGap Demo", 200000);
                db.transaction(populateDB, errorCB, successCB);

                db.transaction(queryDB, errorCB);

            }

            function queryDB(tx) {
                tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
            }

            function querySuccess(tx, results) {
                // this will be empty since no rows were inserted.
                console.log("Insert ID = " + results.insertId);
                // this will be 0 since it is a select statement
                console.log("Rows Affected = " + results.rowAffected);
                // the number of rows returned by the select statement
                console.log("Insert ID = " + results.rows.length);
            }
            // Populate the database
            //
            function populateDB(tx) {
                tx.executeSql('DROP TABLE IF EXISTS DEMO');
                tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
                tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
                tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
            }

            // Transaction error callback
            //
            function errorCB(tx, err) {
                alert("Error processing SQL: "+err);
            }

            // Transaction success callback
            //
            function successCB() {
                alert("success!");
            }

        });
    })

    .config(function ($stateProvider, $urlRouterProvider) {
        "use strict";
        $stateProvider
            .state('app', {
                url: "/app",
                abstract: true,
                templateUrl: "views/menu.html",
                controller: 'AppCtrl'
            })
            .state('app.learn', {
                url: "/learn",
                views: {
                    'menuContent': {
                        templateUrl: "views/learn.html",
                        controller: 'AppCtrl'
                    }
                }
            })

            .state('app.learn.bong', {
                url: "/bong",
                views: {
                    'learn-content': {
                        templateUrl: "views/learn/bong.html"
                    }
                }
            })
            .state('app.learn.giatdien', {
                url: "/giatdien",
                views: {
                    'learn-content': {
                        templateUrl: "views/learn/giatdien.html"
                    }
                }
            })
            .state('app.learn.matmau', {
                url: "/matmau",
                views: {
                    'learn-content': {
                        templateUrl: "views/learn/matmau.html"
                    }
                }
            })
            .state('app.learn.gayxuong', {
                url: "/gayxuong",
                views: {
                    'learn-content': {
                        templateUrl: "views/learn/gayxuong.html"
                    }
                }
            })
            .state('app.learn.nghen', {
                url: "/nghen",
                views: {
                    'learn-content': {
                        templateUrl: "views/learn/nghen.html"
                    }
                }
            })
            .state('app.learn.battinhtho', {
                url: "/battinhtho",
                views: {
                    'learn-content': {
                        templateUrl: "views/learn/battinhtho.html"
                    }
                }
            })
            .state('app.learn.battinhkhongtho', {
                url: "/battinhkhongtho",
                views: {
                    'learn-content': {
                        templateUrl: "views/learn/battinhkhongtho.html"
                    }
                }
            })
            .state('app.prepare', {
                url: "/prepare",
                views: {
                    'menuContent': {
                        templateUrl: "views/prepare.html"
                    }
                }
            })
            .state('app.prepare.tuiyte', {
                url: "/tuiyte",
                views: {
                    'prepare-content': {
                        templateUrl: "views/prepare/tuiyte.html"
                    }
                }
            })
            .state('app.prepare.hoahoan', {
                url: "/hoahoan",
                views: {
                    'prepare-content': {
                        templateUrl: "views/prepare/hoahoan.html"

                    }
                }
            });
        // if none of the above states are matched, use this as the fallback
        $urlRouterProvider.otherwise('/app/learn');
    });

